package base.day16.controller;

import base.day16.domain.Student;
import base.day16.service.StudentService;

import java.util.Scanner;

public  abstract class BaseOtherStudentController {
    //将学生信息传递给StudentService中add方法
    private StudentService studentService=new StudentService();
    Scanner sc=new Scanner(System.in);
    public final void start() {
        wc:while (true) {
            System.out.println("------------<学生>管理系统----------");
            System.out.println("1 添加学生 \t 2 删除学生 \t 3 修改学生 \t 4 查看学生 \t 5 退出");
            System.out.println("请输入你的选择：");
            String st = sc.next();
            switch (st) {
                case "1":
                    System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    System.out.println("删除学生");
                    deldteStudentById();
                    break;
                case "3":
                    System.out.println("修改学生");
                    upDateStudent();
                    break;
                case "4":
                    System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("已退出学生管理系统");
                    break wc;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        }
    }

    public final void upDateStudent() {
        String upateId=inputStudentId();
        Student newstu=inputStudentInfo(upateId);
        studentService.upDateStudent(upateId,newstu);
        System.out.println("修改成功");
    }

    public final void deldteStudentById() {
        String deid=inputStudentId();
        studentService.deldteStudentById(deid);
        System.out.println("删除成功");
    }

    public final void findAllStudent() {
        Student[] stus=studentService.findAllStudent();
        if (stus==null){
            System.out.println("查无信息，请添加后再试");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu=stus[i];
            if (stu!=null){
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getBirthday());
            }
        }
    }

    public final void addStudent() {
        //键盘录入学生信息
        String id;
        while (true){
            System.out.println("请输入学号：");
            id=sc.next();
            boolean flag=studentService.isExists(id);
            if (flag) {
                System.out.println("学号已被占用，请重新输入");
            }
            else {
                break;
            }
        }
        Student stu=inputStudentInfo(id);
        boolean result=studentService.addStudent(stu);
        //根据返回值boolaen判断，是否添加成功
        if (result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
    public final String inputStudentId(){
        String id;
        while (true){
            System.out.println("请输入要学生id");
            id=sc.next();
            boolean exists= studentService.isExists(id);
            if (!exists){
                System.out.println("您输入的id不存在，请重新输入 ");
            }else{
                break;
            }
        }
        return id;
    }
    public  abstract Student inputStudentInfo(String id);
}
